package com.mokairui.basic.callable;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author Mokairui
 * @Description 创建线程的第三种方式Callable
 * @Since 2021/6/6
 */
@Slf4j(topic = "c.Callable")
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                log.debug("running...");
                TimeUnit.SECONDS.sleep(2);
                return 100;
            }
        });

        Thread t1 = new Thread(futureTask, "t1");
        t1.start();

        log.debug("{}", futureTask.get());
    }
}
